package com.wzx.ptplatform.controller;

import com.wzx.ptplatform.domain.*;
import com.wzx.ptplatform.service.*;
import com.wzx.ptplatform.utils.ConstUtils;
import com.wzx.ptplatform.utils.MapUtils;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author 紫煦桉
 */
@Api(tags = "登录模块")
@RestController
public class LoginController {

    final UserService userService;
    final UniversityService universityService;
    final StudentService studentService;
    final CompanyService companyService;
    final WorkExperienceService workExperienceService;
    final ProjectExperienceService projectExperienceService;
    final PartTimeMessageService partTimeMessageService;

    public LoginController(UserService userService, UniversityService universityService, StudentService studentService, CompanyService companyService, WorkExperienceService workExperienceService, ProjectExperienceService projectExperienceService, PartTimeMessageService partTimeMessageService) {
        this.userService = userService;
        this.universityService = universityService;
        this.studentService = studentService;
        this.companyService = companyService;
        this.workExperienceService = workExperienceService;
        this.projectExperienceService = projectExperienceService;
        this.partTimeMessageService = partTimeMessageService;
    }

    @GetMapping("admin_login")
    public ModelAndView adminLogin() {
        return new ModelAndView("admin/login");
    }

    @PostMapping("admin_login_in")
    public Map<String, Object> login(User user, HttpSession session) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (userService.login(user) != null) {
            MapUtils.successMap(map);
            session.setAttribute("user", userService.login(user));
        }
        return map;
    }

    @GetMapping("university_login")
    public ModelAndView universityLogin() {
        return new ModelAndView("university/login");
    }

    @PostMapping("university_login_in")
    public Map<String, Object> login(University university, HttpSession session) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (universityService.login(university) != null) {
            MapUtils.successMap(map);
            session.setAttribute("university", universityService.login(university));
        }
        return map;
    }

    @GetMapping("student_login")
    public ModelAndView studentLogin() {
        return new ModelAndView("student/login");
    }

    @PostMapping("student_login_in")
    public Map<String, Object> login(Student student, HttpSession session) {
        Map<String, Object> map = MapUtils.initializeMap();
        Student studentMessage = studentService.login(student);
        if (studentMessage != null) {
            MapUtils.successMap(map);
            List<WorkExperience> workExperienceList;
            List<ProjectExperience> projectExperienceList;
            if (studentMessage.getResume() != null) {
                workExperienceList = workExperienceService.findList(new WorkExperience(studentMessage.getResume()), null, null).getContent();
                projectExperienceList = projectExperienceService.findList(new ProjectExperience(studentMessage.getResume()), null, null).getContent();
            } else {
                studentMessage.setResume(new Resume("", new Industry(), new Job(), new City(), null, null, ""));
                workExperienceList = Collections.singletonList(new WorkExperience("", new Job(), new City(), null, null, ""));
                projectExperienceList = Collections.singletonList(new ProjectExperience("", "", ""));
            }
            session.setAttribute("workExperienceList", workExperienceList);
            session.setAttribute("projectExperienceList", projectExperienceList);
            session.setAttribute("student", studentMessage);
        }
        return map;
    }

    @GetMapping("company_login")
    public ModelAndView companyLogin() {
        return new ModelAndView("company/login");
    }

    @PostMapping("company_login_in")
    public Map<String, Object> login(Company company, HttpSession session) {
        Map<String, Object> map = MapUtils.initializeMap();
        Company companyMessage = companyService.login(company);
        if (companyMessage != null) {
            MapUtils.successMap(map);
            session.setAttribute("partTimeMessageListReady", partTimeMessageService.findByCompanyId(companyMessage.getId(), ConstUtils.POST_STATUS_READY));
            session.setAttribute("partTimeMessageListNotReady", partTimeMessageService.findByCompanyId(companyMessage.getId(), ConstUtils.POST_STATUS_NOT_READY));
            session.setAttribute("company", companyMessage);
        }
        return map;
    }
}
